﻿using Microsoft.EntityFrameworkCore;

namespace cmdb.keede.entity
{
    public class CMDBContext : DbContext
    {

        public CMDBContext(DbContextOptions<CMDBContext> options)
            : base(options)
        { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ApplicationInfo>(entity =>
            {
                entity.Property(e => e.Name)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Description)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.PICName)
                    .HasColumnName("PICName")
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Port)
                    .IsRequired()
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.Type)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);


                entity.HasOne(d => d.Business)
                    .WithMany(p => p.ApplicationInfo)
                    .HasForeignKey(d => d.BusinessId)
                    .HasConstraintName("FK_APPLICAT_REFERENCE_BUSINESS");

                entity.HasOne(d => d.PIC)
                    .WithMany(p => p.ApplicationInfo)
                    .HasForeignKey(d => d.PICId)
                    .HasConstraintName("FK_APPLICAT_REFERENCE_USER");
            });

            modelBuilder.Entity<ApplicationVirtualHost>(entity =>
            {
                entity.HasKey(t => new { t.ApplicationId, t.VirtualHostId });

                entity.HasOne(d => d.Application)
                    .WithMany(p => p.ApplicationVirtualHosts)
                    .HasForeignKey(d => d.ApplicationId);

                entity.HasOne(d => d.VirtualHost)
                    .WithMany(p => p.ApplicationVirtualHosts)
                    .HasForeignKey(d => d.VirtualHostId);
            });

            modelBuilder.Entity<BusinessInfo>(entity =>
            {
                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.Description)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.PICName)
                    .IsRequired()
                    .HasColumnName("PICName")
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.HasOne(d => d.Environment)
                    .WithMany(p => p.BusinessInfo)
                    .HasForeignKey(d => d.EnvironmentId)
                    .HasConstraintName("FK_BUSINESS_REFERENCE_ENVIRONM");

                entity.HasOne(d => d.PicNavigation)
                    .WithMany(p => p.BusinessInfo)
                    .HasForeignKey(d => d.PICId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_BUSINESS_REFERENCE_USER");
            });

            modelBuilder.Entity<EnvironmentInfo>(entity =>
            {
                entity.Property(e => e.Description)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<Group>(entity =>
            {
                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.CreatorId)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.CreatorName)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Description)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.Status)
                    .IsRequired()
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.HasOne(d => d.CreatorNavigation)
                    .WithMany(p => p.Group)
                    .HasForeignKey(d => d.CreatorId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_GROUP_REFERENCE_USER");
            });

            modelBuilder.Entity<GroupHost>(entity =>
            {

                entity.Property(e => e.RelationType)
                    .HasColumnName("RelationType")
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.RelationKey)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.RelationValue)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.HasOne(d => d.Group)
                    .WithMany(p => p.GroupHost)
                    .HasForeignKey(d => d.GroupId)
                    .HasConstraintName("FK_Group_REF_GroutHost");
            });

            modelBuilder.Entity<OperationLog>(entity =>
            {
                entity.Property(e => e.OperationContent)
                    .IsRequired()
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.OperationTime).HasColumnType("datetime");

                entity.Property(e => e.Operator)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Result)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.HasOne(d => d.OperatorNavigation)
                    .WithMany(p => p.OperationLog)
                    .HasForeignKey(d => d.Operator)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_OPERATIO_REFERENCE_USER");
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.Property(e => e.Id)
                    .HasMaxLength(50)
                    .IsUnicode(false)
                    .ValueGeneratedNever();

                entity.Property(e => e.Email)
                    .HasColumnName("email")
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Password)
                    .IsRequired()
                    .HasMaxLength(128)
                    .IsUnicode(false);

                entity.Property(e => e.Role)
                    .IsRequired()
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.Status)
                    .IsRequired()
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.Tel)
                    .HasMaxLength(20)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<VirtualHostInfo>(entity =>
            {
                entity.Property(e => e.Area)
                    .IsRequired()
                    .HasMaxLength(255)
                    .IsUnicode(false)
                    .HasDefaultValueSql("('')");

                entity.Property(e => e.AssetId)
                    .IsRequired()
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.CPU)
                    .HasColumnName("CPU")
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.CreateTime).HasColumnType("datetime");

                entity.Property(e => e.DeviceType)
                    .HasColumnName("DeviceType")
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.ExpireTime)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("('2099-1-1')");

                entity.Property(e => e.HardDiskSize)
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.InnerIP)
                .HasMaxLength(50)
                .IsUnicode(false);

                entity.Property(e => e.Maintainers)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.NetworkBandwidth).HasDefaultValueSql("((1000))");

                entity.Property(e => e.OS)
                    .HasColumnName("OS")
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.OuterIP)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Password)
                    .HasMaxLength(256)
                    .IsUnicode(false);

                entity.Property(e => e.RAM)
                    .HasColumnName("RAM")
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.Remark)
                    .HasMaxLength(255)
                    .IsUnicode(false);

                entity.Property(e => e.Status)
                    .HasColumnName("Status")
                    .HasDefaultValueSql("((0))");

                entity.Property(e => e.AdminName)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.HasOne(d => d.MaintainersNavigation)
                    .WithMany(p => p.VirtualHostInfo)
                    .HasForeignKey(d => d.Maintainers)
                    .HasConstraintName("FK_VIRTUALH_REFERENCE_USER");

                entity.Property(e => e.OSVersion)
                    .HasMaxLength(20)
                    .IsUnicode(false);
            });
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

        public virtual DbSet<ApplicationInfo> ApplicationInfo { get; set; }
        public virtual DbSet<BusinessInfo> BusinessInfo { get; set; }
        public virtual DbSet<EnvironmentInfo> EnvironmentInfo { get; set; }
        public virtual DbSet<Group> Group { get; set; }
        public virtual DbSet<GroupHost> GroupHost { get; set; }
        public virtual DbSet<OperationLog> OperationLog { get; set; }
        public virtual DbSet<User> User { get; set; }
        public virtual DbSet<VirtualHostInfo> VirtualHostInfo { get; set; }
    }
}
